using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.DependencyInjection;
using HanumanInstitute.MvvmDialogs;
using HanumanInstitute.MvvmDialogs.FrameworkDialogs;
using AvaloniaUiKit.SampleApp.ViewModels;
using HanumanInstitute.MvvmDialogs.FileSystem;

namespace AvaloniaUiKit.SampleApp.Extensions
{
    public static class DialogServiceExtensions
    {
        /// <summary>
        /// Shows a message box with the main window as the owner, automatically resolved from the IoC container.
        /// </summary>
        /// <param name="dialogService">The dialog service instance.</param>
        /// <param name="text">The text to display in the message box.</param>
        /// <param name="title">The title of the message box.</param>
        /// <param name="buttons">The buttons to display in the message box. Default is OK button.</param>
        /// <param name="icon">The icon to display in the message box. Default is no icon.</param>
        /// <returns>A task that completes when the message box is closed, containing the user's response.</returns>
        public static Task<bool?> ShowMessageBoxAsync(
            this IDialogService dialogService,
            string text,
            string title = "",
            MessageBoxButton buttons = MessageBoxButton.Ok,
            MessageBoxImage icon = MessageBoxImage.None)
        {
            var owner = Ioc.Default.GetService<MainWindowViewModel>();
            return dialogService.ShowMessageBoxAsync(owner, text, title, buttons, icon);
        }

        /// <summary>
        /// Shows a dialog with the main window as the owner, automatically resolved from the IoC container.
        /// </summary>
        /// <typeparam name="T">The return type of the dialog.</typeparam>
        /// <param name="dialogService">The dialog service instance.</param>
        /// <param name="viewModel">The view model of the dialog to show.</param>
        /// <returns>The dialog result.</returns>
        public static Task<bool?> ShowDialogAsync<T>(
            this IDialogService dialogService,
            IModalDialogViewModel viewModel)
        {
            var owner = Ioc.Default.GetService<MainWindowViewModel>();
            return dialogService.ShowDialogAsync<T>(owner, viewModel);
        }

        /// <summary>
        /// Shows an open file dialog with the main window as the owner, automatically resolved from the IoC container.
        /// </summary>
        /// <param name="dialogService">The dialog service instance.</param>
        /// <param name="settings">The settings for the open file dialog.</param>
        /// <returns>An array of selected file paths, or null if the dialog was canceled.</returns>
        public static Task<IDialogStorageFile  ?> ShowOpenFileDialogAsync(
            this IDialogService dialogService,
            OpenFileDialogSettings? settings = null)
        {
            var owner = Ioc.Default.GetService<MainWindowViewModel>();
            return dialogService.ShowOpenFileDialogAsync(owner, settings);
        }

        /// <summary>
        /// Shows a save file dialog with the main window as the owner, automatically resolved from the IoC container.
        /// </summary>
        /// <param name="dialogService">The dialog service instance.</param>
        /// <param name="settings">The settings for the save file dialog.</param>
        /// <returns>The selected file path, or null if the dialog was canceled.</returns>
        public static Task<IDialogStorageFile?> ShowSaveFileDialogAsync(
            this IDialogService dialogService,
            SaveFileDialogSettings? settings = null)
        {
            var owner = Ioc.Default.GetService<MainWindowViewModel>();
            return dialogService.ShowSaveFileDialogAsync(owner, settings);
        }

        /// <summary>
        /// Shows a folder browser dialog with the main window as the owner, automatically resolved from the IoC container.
        /// </summary>
        /// <param name="dialogService">The dialog service instance.</param>
        /// <param name="settings">The settings for the folder browser dialog.</param>
        /// <returns>The selected directory path, or null if the dialog was canceled.</returns>
        public static Task<IDialogStorageFolder?> ShowOpenFolderDialogAsync(
            this IDialogService dialogService,
            OpenFolderDialogSettings? settings = null)
        {
            var owner = Ioc.Default.GetService<MainWindowViewModel>();
            return dialogService.ShowOpenFolderDialogAsync(owner, settings);
        }
    }
}
